# @Time    :2025/3/27 9:19
# matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# transposed = [[row[i] for row in matrix] for i in range(len(matrix))]
# print(transposed)


# 1. 编写程序，将一个多维列表扁平化。例如，将[[1, 2], [3, [4, 5]], 6]转换为[1, 2, 3, 4, 5, 6]。

# def flatten(lst):
#     result = []
#     for item in lst:
#         if isinstance(item, list):
#             result.extend(flatten(item))
#         else:
#             result.append(item)
#     return result
#
# test_list = [[1, 2], [3, [4, 5]], 6]
# print(flatten(test_list))

# 2. 用户输入一段文本，统计文本中每个单词出现的次数，输出出现频率最高的前3个单词及其出现次数。可以假设单词之间用空格分隔，不考虑标点符号。

text = input("请输入文本：").split()
word_count = {}
for word in text:
    word_count[word] = word_count.get(word, 0) + 1
sorted_word_count = sorted(word_count.items(), key=lambda item: item[1], reverse=True)
for i in range(3):
    print(f"单词：{sorted_word_count[i][0]}，出现次数：{sorted_word_count[i][1]}")
